package company

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"jwgin/model"
	"jwgin/model/mysql"
	"jwgin/service"
	"jwgin/utils/jw"
	"jwgin/utils/sign"
)

type Login struct{}

func (l *Login) Routers(Router *gin.RouterGroup) (R gin.IRoutes) {
	Router.POST("login", l.login)
	return Router
}

func (Login) login(c *gin.Context) {
	requ := &model.RequLogin{}
	if err := c.ShouldBind(requ); err != nil {
		RespErr(c, err)
		return
	}

	users := &mysql.Company{}
	service.G.Where("account = ? ", requ.Account).First(users)
	// 密码校验
	if md5 := sign.HmacSha256(users.Salt, requ.Password); md5 != users.Password {
		fmt.Println(requ.Password, "=>", md5)
		RespErr(c, fmt.Errorf("账号或密码错误"))
		return
	}
	fmt.Println(users.Id)
	//返回token
	token := jw.SetToken(uint(users.Id), uint(users.Id))
	ret := model.RespLogin{
		Username: users.Title,
		Account:  users.Account,
		Token:    token,
	}
	RespSucc(c, ret)
}
